package com.gopay.webtest.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.P2PSignHandler;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.P2PBindParamVO;
import com.gopay.webtest.vo.P2PRechargeParamVO;
import com.gopay.webtest.vo.P2PTransferParamVO;
import com.gopay.webtest.xml.XmlDao;

/**
 * P2P Controller
 * @P2PController.java
 * @author fanghw
 * @2014-4-18 上午9:53:29  www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/p2p")
@Controller
public class P2PController extends BaseController {
    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "p2PSignHandler")
    private P2PSignHandler p2PSignHandler;
    
    /**
     * 跳转到转账页面
     * 
     * @return
     */
    @RequestMapping("/transfer")
    public ModelAndView transfer() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("p2p_transfer", "transfer");

            
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("p2p.transfer");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到转账页面", e);
        }

        return mv;
    }

    /**
     * 执行转账
     * 
     * @return
     */
    @RequestMapping("/doTransfer")
    public ModelAndView doTransfer(@ModelAttribute P2PTransferParamVO paramVo) {
        ModelAndView mv = new ModelAndView();

        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl();
            if(backgroundMerUrl!=null&&!backgroundMerUrl.equals("")){
                backgroundMerUrl = backgroundMerUrl+ "?id="+ paramVo.getVerficationCode();
            }
            
            String frontMerUrl = paramVo.getFrontMerUrl();
            if(frontMerUrl!=null&&!frontMerUrl.equals("")){
                frontMerUrl = frontMerUrl+ "?id="
                        + paramVo.getVerficationCode();
            }
            paramVo.setBackgroundMerUrl(backgroundMerUrl);
            paramVo.setFrontMerUrl(frontMerUrl);
            
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
            paramVo.setGopayServerTime(paramVo.getTranDateTime());
            
            // 签名
            p2PSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            
           
            
            mv.setViewName("p2p.doTransfer");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行转账", e);
        }

        return mv;
    }
    
    /**
     * 跳转到充值页面
     * 
     * @return
     */
    @RequestMapping("/recharge")
    public ModelAndView recharge() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("p2p_recharge", "recharge");

            
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("p2p.recharge");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到充值页面", e);
        }

        return mv;
    }

    /**
     * 执行充值
     * 
     * @return
     */
    @RequestMapping("/doRecharge")
    public ModelAndView doRecharge(@ModelAttribute P2PRechargeParamVO paramVo) {
        ModelAndView mv = new ModelAndView();

        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl();
            if(backgroundMerUrl!=null&&!backgroundMerUrl.equals("")){
                backgroundMerUrl = backgroundMerUrl+ "?id="+ paramVo.getVerficationCode();
            }
            
            String frontMerUrl = paramVo.getFrontMerUrl();
            if(frontMerUrl!=null&&!frontMerUrl.equals("")){
                frontMerUrl = frontMerUrl+ "?id="
                        + paramVo.getVerficationCode();
            }
            paramVo.setBackgroundMerUrl(backgroundMerUrl);
            paramVo.setFrontMerUrl(frontMerUrl);
            
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
            paramVo.setGopayServerTime(paramVo.getTranDateTime());
            
            // 签名
            p2PSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());

            mv.setViewName("p2p.doRecharge");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行充值", e);
        }

        return mv;
    }
    
    /**
     * 跳转到提现页面
     * 
     * @return
     */
    @RequestMapping("/draw")
    public ModelAndView draw() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("p2p_draw", "draw");

            
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("p2p.draw");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到提现页面", e);
        }

        return mv;
    }

    /**
     * 执行提现
     * 
     * @return
     */
    @RequestMapping("/doDraw")
    public ModelAndView doDraw(@ModelAttribute P2PRechargeParamVO paramVo) {
        ModelAndView mv = new ModelAndView();

        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl();
            if(backgroundMerUrl!=null&&!backgroundMerUrl.equals("")){
                backgroundMerUrl = backgroundMerUrl+ "?id="+ paramVo.getVerficationCode();
            }
            
            String frontMerUrl = paramVo.getFrontMerUrl();
            if(frontMerUrl!=null&&!frontMerUrl.equals("")){
                frontMerUrl = frontMerUrl+ "?id="
                        + paramVo.getVerficationCode();
            }
            paramVo.setBackgroundMerUrl(backgroundMerUrl);
            paramVo.setFrontMerUrl(frontMerUrl);
            
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
            paramVo.setGopayServerTime(paramVo.getTranDateTime());
            
            // 签名
            p2PSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            
           
            
            mv.setViewName("p2p.doDraw");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行提现", e);
        }

        return mv;
    }
    /**
     * 跳转到签约页面
     * 
     * @return
     */
    @RequestMapping("/bind")
    public ModelAndView bind() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("p2p_bind", "bind");

            
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("p2p.bind");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到签约页面", e);
        }

        return mv;
    }
    /**
     * 执行签约
     * 
     * @return
     */
    @RequestMapping("/doBind")
    public ModelAndView doBind(@ModelAttribute P2PBindParamVO paramVo) {
        ModelAndView mv = new ModelAndView();

        try {
            String backgroundMerUrl = paramVo.getBackgroundMerUrl();
            if(backgroundMerUrl!=null&&!backgroundMerUrl.equals("")){
                backgroundMerUrl = backgroundMerUrl+ "?id="+ paramVo.getVerficationCode();
            }
            
            String frontMerUrl = paramVo.getFrontMerUrl();
            if(frontMerUrl!=null&&!frontMerUrl.equals("")){
                frontMerUrl = frontMerUrl+ "?id="
                        + paramVo.getVerficationCode();
            }
            paramVo.setBackgroundMerUrl(backgroundMerUrl);
            paramVo.setFrontMerUrl(frontMerUrl);
            
            //国付宝服务器时间,本应该从网关time接口获取,此处为方便测试,取页面上的可输入时间
            paramVo.setGopayServerTime(paramVo.getTranDateTime());
            
            // 签名
            p2PSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo, getRequest());
            
           
            
            mv.setViewName("p2p.doBind");
            mv.addObject("paramVo", paramVo);
        } catch (Exception e) {
            logger.error("执行提现", e);
        }

        return mv;
    }
}
